package cn.edu.gduf.luowowo.website.web.controller;

import cn.edu.gduf.luowowo.article.query.TravelQuery;
import cn.edu.gduf.luowowo.article.service.IStrategyCommendService;
import cn.edu.gduf.luowowo.article.service.ITravelCommendService;
import cn.edu.gduf.luowowo.article.service.ITravelService;
import cn.edu.gduf.luowowo.search.domain.DestinationTemplate;
import cn.edu.gduf.luowowo.search.domain.StrategyTemplate;
import cn.edu.gduf.luowowo.search.domain.TravelTemplate;
import cn.edu.gduf.luowowo.search.domain.UserInfoTemplate;
import cn.edu.gduf.luowowo.search.query.SearchQueryObject;
import cn.edu.gduf.luowowo.search.service.*;
import cn.edu.gduf.luowowo.search.vo.SearchResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 访问首页
 */
@Controller
public class IndexController {
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private ISearchService searchService;

    // ""空引号，表示访问根目录，就是直接ip+端口
    @RequestMapping("")
    public String index(Model model) {
        //tcs 首页的游记推荐
        model.addAttribute("tcs", travelCommendService.queryTop5());
        //sc 攻略推荐，第一个
        model.addAttribute("sc", strategyCommendService.queryTop5().get(2));
        return "index/index";
    }

    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, TravelQuery qo) {
        qo.setPageSize(5);
        model.addAttribute("pageInfo", travelService.query(qo));
        return "index/travelPageTpl";
    }

    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo") SearchQueryObject qo) {
        if (qo.getKeyword() != null) {
            switch (qo.getType()) {
                case SearchQueryObject.SEARCH_TYPE_DESTINATION:
                    return searchDestination(model, qo);
                case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                    return searchStrategy(model, qo);
                case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                    return searchTravel(model, qo);
                case SearchQueryObject.SEARCH_TYPE_USER:
                    return searchUser(model, qo);
                default:
                    return searchAll(model, qo);
            }
        }
        return "index/searchAll";
    }

    private String searchAll(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class, qo, "name", "info");
        long total = dests.getTotalElements() + users.getTotalElements() + travels.getTotalElements() + strategys.getTotalElements();
        SearchResult data = new SearchResult(total, strategys.getContent(), travels.getContent(), users.getContent(), dests.getContent());
        model.addAttribute("data", data);
        return "index/searchAll";
    }

    private String searchUser(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> userInfoTemplates = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page", userInfoTemplates);
        return "index/searchUser";
    }

    private String searchTravel(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> travelTemplates = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page", travelTemplates);
        return "index/searchTravel";
    }

    private String searchStrategy(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> strategyTemplates = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page", strategyTemplates);
        return "index/searchStrategy";
    }

    private String searchDestination(Model model, SearchQueryObject qo) {
        String destName = qo.getKeyword();
        //通过关键字查询目的地是否存在
        DestinationTemplate destinationTemplate = destinationSearchService.getDestByDestName(destName);
        if (destinationTemplate != null) {
            //存在，接着查出该目的地下的所有攻略、游记和用户，统一就只要前5条数据
            List<StrategyTemplate> sts = strategySearchService.getStrategyByDestName(destName);
            List<TravelTemplate> ts = travelSearchService.getTravelByDestName(destName);
            List<UserInfoTemplate> us = userInfoSearchService.getUserByDestName(destName);
            int total = sts.size() + ts.size() + us.size();
            SearchResult data = new SearchResult(total, sts, ts, us, null);
            if (sts.size() > 5) {
                sts.subList(0, 5);
            }
            if (ts.size() > 5) {
                ts.subList(0, 5);
            }
            if (us.size() > 5) {
                us.subList(0, 5);
            }
            model.addAttribute("data", data);
        }
        model.addAttribute("dest", destinationTemplate);
        //不存在则不做其他操作
        return "index/searchDest";
    }

}
